(async () => {
  const Sequelize = require("sequelize");
  // 建⽴连接
  const sequelize = new Sequelize("kaikeba", "root", "admin", {
    host: "localhost",
    dialect: "mysql",
    operatorsAliases: false,
  });
  // 定义模型：kaikeba数据库创建表fruit_tables
  const Fruit = sequelize.define("FruitTable", {
    id: {
      type: Sequelize.DataTypes.UUID,
      defaultValue: Sequelize.DataTypes.UUIDV1,
      primaryKey: true
    },
    name: {
      type: Sequelize.STRING(20),
      allowNull: false
    },
    price: {
      type: Sequelize.FLOAT,
      allowNull: false
    },
    stock: {
      type: Sequelize.INTEGER,
      defaultValue: 0
    },
  }, {
    timestamps: false, // 避免⾃动⽣成时间戳字段
    tableName: 'TBL_FRUIT', // 指定表名(此时kaikeba数据库会生成tbl_fruit数据表)
    // underscored: true, // 假如此时此刻数据表名为FruitTable，那么就会在数据库中生成fruit_tables数据表
  });
  // 强制同步：创建表之前先删除已存在的表
  let ret = await Fruit.sync({
    force: true
  });
  console.log("sync", ret);

  ret = await Fruit.create({
    name: "雪梨",
    price: 3.5,
  });
  console.log("create", ret);

  ret = await Fruit.findAll();
  await Fruit.update({
    price: 4
  }, {
    where: {
      name: "⾹蕉"
    }
  });
  console.log("findAll", JSON.stringify(ret));

  // 含条件的查询
  const Op = Sequelize.Op;
  ret = await Fruit.findAll({
    where: {
      price: {
        [Op.lt]: 6,
        [Op.gt]: 2
      }
    },
  });
  console.log("findAll-price(2~6)", JSON.stringify(ret, "", "\t"));
})();
